﻿using CoreCms.Net.Bz.Common.Response;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System.Net;

namespace CoreCms.Net.Bz.Filter
{
    /// <summary>
    /// 接口全局异常错误日志
    /// </summary>
    public class GlobalExceptionsFilterForAdmin : IExceptionFilter
    {
        private readonly ILogger _logger;

        public GlobalExceptionsFilterForAdmin(ILogger<GlobalExceptionsFilterForAdmin> logger)
        {
            _logger = logger;
        }

        public void OnException(ExceptionContext context)
        {
            // NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.Web, "全局异常", "全局捕获异常", context.Exception);
            _logger.LogError(Guid.NewGuid().ToString(), context.Exception);
            HttpStatusCode status = HttpStatusCode.InternalServerError;

            //处理各种异常
            var jm = new AdminUiCallBack();
            jm.code = (int)status;
            jm.msg = "系统异常，请查看错误描述并进行解决。";
            jm.data = context.Exception;
            context.ExceptionHandled = true;
            context.Result = new ObjectResult(jm);
        }
    }
}